package cn.iocoder.yudao.module.of.controller.admin.taskgenuser;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.of.controller.admin.taskgenuser.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.taskgenuser.TaskGenUserDO;
import cn.iocoder.yudao.module.of.service.taskgenuser.TaskGenUserService;

@Tag(name = "管理后台 - 会议、收文  主责部门指派任务生成人员")
@RestController
@RequestMapping("/of/task-gen-user")
@Validated
public class TaskGenUserController {

    @Resource
    private TaskGenUserService taskGenUserService;

    @PostMapping("/create")
    @Operation(summary = "创建会议、收文  主责部门指派任务生成人员")
    @PreAuthorize("@ss.hasPermission('of:task-gen-user:create')")
    public CommonResult<Long> createTaskGenUser(@Valid @RequestBody TaskGenUserSaveReqVO createReqVO) {
        return success(taskGenUserService.createTaskGenUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会议、收文  主责部门指派任务生成人员")
    @PreAuthorize("@ss.hasPermission('of:task-gen-user:update')")
    public CommonResult<Boolean> updateTaskGenUser(@Valid @RequestBody TaskGenUserSaveReqVO updateReqVO) {
        taskGenUserService.updateTaskGenUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会议、收文  主责部门指派任务生成人员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('of:task-gen-user:delete')")
    public CommonResult<Boolean> deleteTaskGenUser(@RequestParam("id") Long id) {
        taskGenUserService.deleteTaskGenUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会议、收文  主责部门指派任务生成人员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:task-gen-user:query')")
    public CommonResult<TaskGenUserRespVO> getTaskGenUser(@RequestParam("id") Long id) {
        TaskGenUserDO taskGenUser = taskGenUserService.getTaskGenUser(id);
        return success(BeanUtils.toBean(taskGenUser, TaskGenUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得会议、收文  主责部门指派任务生成人员分页")
    @PreAuthorize("@ss.hasPermission('of:task-gen-user:query')")
    public CommonResult<PageResult<TaskGenUserRespVO>> getTaskGenUserPage(@Valid TaskGenUserPageReqVO pageReqVO) {
        PageResult<TaskGenUserDO> pageResult = taskGenUserService.getTaskGenUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TaskGenUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出会议、收文  主责部门指派任务生成人员 Excel")
    @PreAuthorize("@ss.hasPermission('of:task-gen-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTaskGenUserExcel(@Valid TaskGenUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TaskGenUserDO> list = taskGenUserService.getTaskGenUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会议、收文  主责部门指派任务生成人员.xls", "数据", TaskGenUserRespVO.class,
                        BeanUtils.toBean(list, TaskGenUserRespVO.class));
    }

}